package cn.hznc.controller.service.inventory;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hznc.controller.task.InventoryTask;
import cn.hznc.controller.service.toollife.ToolLieService;
import cn.hznc.domain.entity.*;
import cn.hznc.domain.request.inventory.*;
import cn.hznc.domain.request.toolpart.EditToolPartLengthAndRadiusReq;
import cn.hznc.domain.response.PageResult;
import cn.hznc.exception.ToolLifeCycleEx;
import cn.hznc.exception.ToolLifeCycleException;
import cn.hznc.mapper.AssemblyToolInventoryMapper;
import cn.hznc.mapper.ICabinetRecordMapper;
import cn.hznc.mapper.ToolInventoryLocationMapper;
import cn.hznc.mapper.ToolPartMapper;
import cn.hznc.repository.*;
import cn.hznc.utils.CommonUtil;
import cn.hznc.vo.*;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author：Administrator
 * @Date：2023/4/9 21:39
 */
@Service
public class AssemblyToolInventoryService{

    @Resource
    private InventoryTask inventoryTask;

    @Resource
    private ICabinetRecordMapper iCabinetRecordMapper;
    @Resource
    private ToolInventoryLocationRespository toolInventoryLocationRespository;

    @Resource
    private ToolInventoryLocationMapper toolInventoryLocationMapper;

    @Resource
    private InventoryLocationRespository inventoryLocationRespository;

    @Resource
    private ToolLieService toolLieService;

    @Resource
    private ToolPartRespository toolPartRespository;

    @Resource
    private WarehouseRespository warehouseRespository;

    @Resource
    private AssemblyToolInventoryMapper assemblyToolInventoryMapper;

    @Resource
    private ToolForkInfoRespository toolForkInfoRespository;

    @Resource
    private ToolInventoryLocationHistoryRepository toolInventoryLocationHistoryRepository;

    @Resource
    private AssemblyToolInventoryRepository assemblyToolInventoryRepository;

    @Resource
    private ToolPartMapper toolPartMapper;


    @Autowired
    private ToolInventoryLocationHistoryService toolInventoryLocationHistoryService;

    @Autowired
    private MinioProp minioProp;

    public Map<String, Object> getAssemblyToolManageList(ToolPartsManageReq toolPartsManageReq){
        Map<String, Object> retMap = new HashMap<>();
        //PageHelper.startPage(toolPartsManageReq.getPageNum(), toolPartsManageReq.getPageSize());
        //PageInfo<AssemblyToolInventoryVo> pageInfo = new PageInfo<>();
        List<AssemblyToolInventoryVo> list = toolInventoryLocationRespository.selectAssemblyToolList(toolPartsManageReq);
        if(!CollectionUtils.isEmpty(list)){
           list.forEach(this::buildAssemblyToolInventoryVo);
            //按照安全库存过滤状态
            if(ObjectUtil.isNotNull(toolPartsManageReq.getLocationStatus())){
                list = list.stream().filter(toolInventoryVo -> toolPartsManageReq.getLocationStatus().equals(toolInventoryVo.getStatus())).collect(Collectors.toList());
            }
        }
        //手动分页
        List<AssemblyToolInventoryVo> pageList = new ArrayList<>();
        if(CollectionUtil.isNotEmpty(list)){
            list = list.stream().sorted(Comparator.comparing(AssemblyToolInventoryVo::getPhysicalInventory)).collect(Collectors.toList());
            pageList = CommonUtil.pageList(list, toolPartsManageReq.getPageNum(), toolPartsManageReq.getPageSize());
        }
        retMap.put("list", pageList);
        retMap.put("total", list.size());
        return retMap;
    }

    private void buildAssemblyToolInventoryVo(AssemblyToolInventoryVo assemblyToolInventoryVo) {
        if(ObjectUtil.isNotNull(assemblyToolInventoryVo.getImage()) || StringUtil.isNotEmpty(assemblyToolInventoryVo.getImage())){
            assemblyToolInventoryVo.setImage(minioProp.getBaseUrl() + minioProp.getBucketName() + "/" +assemblyToolInventoryVo.getImage());
        }
        assemblyToolInventoryVo.setLocationNum(inventoryLocationRespository.countLocationNum(assemblyToolInventoryVo.getWarehouseId()));
        WarehouseEntity warehouseEntity = warehouseRespository.getByIdOrName(assemblyToolInventoryVo.getWarehouseId() , "");
        assemblyToolInventoryVo.setLocationName(toolLieService.getLocationNameByLocationId(assemblyToolInventoryVo.getInventoryLocationId() , ""));
        String safetyInventoryConfigureParam = warehouseEntity.getSafetyInventoryConfigureParam();
        if(ObjectUtil.isNull(safetyInventoryConfigureParam) || safetyInventoryConfigureParam.equals("[]")){
            return;
        }
        JSONArray.parseArray(safetyInventoryConfigureParam , SingleWarehouseSafetyInventoryParam.class)
                .forEach(singleWarehouseSafetyInventoryParam -> {
                    if(Objects.equals(singleWarehouseSafetyInventoryParam.getToolNo(), assemblyToolInventoryVo.getToolNo())){
                        assemblyToolInventoryVo.setSafetyInventory(singleWarehouseSafetyInventoryParam.getSafetyInventory());
                        if (assemblyToolInventoryVo.getSafetyInventory() >= assemblyToolInventoryVo.getPhysicalInventory()) {
                            assemblyToolInventoryVo.setStatus(0);
                        } else {
                            assemblyToolInventoryVo.setStatus(1);
                        }
                    }
                });
    }

    public Object getAssemblyToolDetailList(InventoryManageDetailReq inventoryManageDetailReq) {
        Map<String,Object> retMap = new HashMap<>();
        Integer id = inventoryManageDetailReq.getAssemblyToolId();
        QueryWrapper<AssemblyToolInventoryEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("id" , id);
        AssemblyToolInventoryEntity assemblyToolInventoryEntity = assemblyToolInventoryMapper.selectOne(wrapper);
        //类型基础信息
        retMap.put("name",assemblyToolInventoryEntity.getToolNo());
        PageHelper.startPage(inventoryManageDetailReq.getPageNum(),inventoryManageDetailReq.getPageSize());
        PageInfo<EntryAndExitRecordVo> pageInfo = new PageInfo<>(toolInventoryLocationRespository.getEntryAndExitRecord(inventoryManageDetailReq));
        //总成材料清单信息
        List<Map<String, Object>> materialList = buildMaterialList(assemblyToolInventoryEntity);
        retMap.put("materialList",materialList);
        //出入库记录表
        retMap.put("recordList",pageInfo.getList());
        retMap.put("total",pageInfo.getTotal());
        //库存情况
        List<Map<String, Object>> locationNum = toolInventoryLocationRespository.getLocationNum(inventoryManageDetailReq);
        locationNum.forEach(stringIntegerMap -> {
            for (Map.Entry<String, Object> stringIntegerEntry : stringIntegerMap.entrySet()) {
                if("warehouseName".equals(stringIntegerEntry.getKey())){
                    WarehouseEntity warehouseEntity = warehouseRespository.getByIdOrName(null , (String) stringIntegerMap.get(stringIntegerEntry.getKey()));
                    if(ObjectUtil.isNull(warehouseEntity)){
                        return;
                    }
                    String safetyInventoryConfigureParam = warehouseEntity.getSafetyInventoryConfigureParam();
                    if(ObjectUtil.isNull(safetyInventoryConfigureParam) || safetyInventoryConfigureParam.equals("[]")){
                        return;
                    }
                    JSONArray.parseArray(safetyInventoryConfigureParam , SingleWarehouseSafetyInventoryParam.class)
                            .forEach(singleWarehouseSafetyInventoryParam -> {
                                if(Objects.equals(singleWarehouseSafetyInventoryParam.getToolNo(), assemblyToolInventoryEntity.getToolNo())){
                                    stringIntegerMap.put("safetyInventory" , singleWarehouseSafetyInventoryParam.getSafetyInventory());
                                }
                            });
                }
            }
        });
        retMap.put("inventoryStatus",locationNum);
        //刀具库存情况统计
        List<Map<String, Object>> inventorySnapshot = toolInventoryLocationHistoryService.getInventorySnapshot(inventoryManageDetailReq.getToolId(), inventoryManageDetailReq.getAssemblyToolId());
        Map<String,Object> nowDate = new HashMap<>();
        nowDate.put("date",new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
        nowDate.put("inventoryStatus",locationNum);
        inventorySnapshot.add(nowDate);
        retMap.put("dateList",inventorySnapshot);
        return retMap;
    }



    public Map<String , Object> getLocationAssemblyToolDetail(LocationToolDetailReq locationToolDetailReq) throws ToolLifeCycleException {
        if(ObjectUtil.isNull(locationToolDetailReq.getId())){
            throw new ToolLifeCycleException(ToolLifeCycleEx.IDNR_NO_EXIT.getCode(),ToolLifeCycleEx.IDNR_NO_EXIT.getMsg());
        }
        //查询总成信息
        Integer id = toolInventoryLocationRespository.queryAssemblyToolId(locationToolDetailReq.getId());
        Map<String , Object> retMap = new HashMap<>();
        QueryWrapper<AssemblyToolInventoryEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("id" , id);
        AssemblyToolInventoryEntity assemblyToolInventoryEntity = assemblyToolInventoryMapper.selectOne(wrapper);
        List<Map<String , Object>> materialList = buildMaterialList(assemblyToolInventoryEntity);
        retMap.put("materialList",materialList);
        retMap.put("name",assemblyToolInventoryEntity.getName());
        retMap.put("type",assemblyToolInventoryEntity.getType());
        retMap.put("image",minioProp.getBaseUrl()+minioProp.getBucketName()+"/"+assemblyToolInventoryEntity.getImage());
//        PageHelper.startPage(locationToolDetailReq.getPageNum(),locationToolDetailReq.getPageSize());
//        PageInfo<ToolPartVo> toolPartVoPageInfo = new PageInfo<>(toolPartRespository.getLocationToolDetail(locationToolDetailReq));
        List<ToolPartVo> locationToolsDetail = toolPartRespository.getLocationToolsDetail(locationToolDetailReq);
        List<ToolPartVo> pageList = new ArrayList<>();
        if(CollectionUtil.isNotEmpty(locationToolsDetail)){
            locationToolsDetail.forEach(toolPartVo -> {
                locationToolDetailReq.setUniqueId(toolPartVo.getUniqueId());
                ToolPartVo oneResult = toolPartRespository.getLocationToolDetail(locationToolDetailReq);
                if(ObjectUtil.isNotNull(oneResult)){
                    toolPartVo.setInboundTime(oneResult.getInboundTime());
                }
            });
            if(ObjectUtil.isNotNull(locationToolDetailReq.getBeginTime())){
                //根据时间过滤
                locationToolsDetail = locationToolsDetail.stream().filter(toolPartVo -> ObjectUtil.isNotNull(toolPartVo.getInboundTime())).collect(Collectors.toList());
                locationToolsDetail = locationToolsDetail.stream().filter(toolPartVo -> toolPartVo.getInboundTime().after(locationToolDetailReq.getBeginTime()) && locationToolDetailReq.getEndTime().after(toolPartVo.getInboundTime())).collect(Collectors.toList());
            }
        }
        if(CollectionUtil.isNotEmpty(locationToolsDetail)){
            //手动分页
            pageList = CommonUtil.pageList(locationToolsDetail,locationToolDetailReq.getPageNum() , locationToolDetailReq.getPageSize());
            if(CollectionUtil.isNotEmpty(pageList)){
                pageList.forEach(toolPartVo -> {
                    if (StringUtil.isNotEmpty(toolPartVo.getImage())) {
                        toolPartVo.setImage(minioProp.getBaseUrl() + minioProp.getBucketName() + "/" + toolPartVo.getImage());
                    }
                });
            }
        }
        retMap.put("list",pageList);
        retMap.put("total",locationToolsDetail.size());
        return retMap;
    }

    private List<Map<String , Object>> buildMaterialList(AssemblyToolInventoryEntity assemblyToolInventoryEntity) {
        List<Map<String , Object>> materialList = new ArrayList<>();
        Map<String , Object> toolMap = new HashMap<>();
        toolMap.put("type" , assemblyToolInventoryEntity.getToolType());
        String toolPartno = getPartnoByPartType(assemblyToolInventoryEntity.getToolType());
        toolMap.put("partno" , toolPartno);
        toolMap.put("number" , assemblyToolInventoryEntity.getToolNumber());
        materialList.add(toolMap);
        Map<String , Object> shankMap = new HashMap<>();
        String shankPartno = getPartnoByPartType( assemblyToolInventoryEntity.getShankType());
        shankMap.put("type" , assemblyToolInventoryEntity.getShankType());
        shankMap.put("partno" , shankPartno);
        shankMap.put("number" , assemblyToolInventoryEntity.getShankNumber());
        materialList.add(shankMap);
        return materialList;
    }

    private String getPartnoByPartType(String toolType) {
        List<String> partnoList = toolForkInfoRespository.selectPartnoByPartType(toolType);
        if(CollectionUtil.isNotEmpty(partnoList)){
            return partnoList.get(0);
        }
        return "";
    }

    public Object getAssemblyToolShelvesManageList(ToolPartsManageReq toolPartsManageReq) {
        Map<String, Object> retMap = new HashMap<>();
        List<AssemblyToolInventoryVo>  resultList = toolInventoryLocationRespository.selectAssemblyToolShelvesList(toolPartsManageReq);
        //PageHelper.startPage(toolPartsManageReq.getPageNum(), toolPartsManageReq.getPageSize());
        //PageInfo<ToolInventoryVo> pageInfo = new PageInfo<>(toolInventoryLocationRespository.getToolPartsManageShelvesList(toolPartsManageReq));
        //设置库位所在仓库对应的库位数量以及库位的安全库存
        if(!CollectionUtils.isEmpty(resultList)){
            resultList.forEach(this::buildAssemblyToolInventoryVo);
            //按照安全库存过滤状态
            if(ObjectUtil.isNotNull(toolPartsManageReq.getLocationStatus())){
                resultList = resultList.stream().filter(toolInventoryVo -> toolPartsManageReq.getLocationStatus().equals(toolInventoryVo.getStatus())).collect(Collectors.toList());
            }
        }
        //手动分页
        List<AssemblyToolInventoryVo> pageList = new ArrayList<>();
        if(CollectionUtil.isNotEmpty(resultList)){
            resultList = resultList.stream().sorted(Comparator.comparing(AssemblyToolInventoryVo::getPhysicalInventory)).collect(Collectors.toList());
            pageList = CommonUtil.pageList(resultList, toolPartsManageReq.getPageNum(), toolPartsManageReq.getPageSize());
        }
        retMap.put("list", pageList);
        retMap.put("total", resultList.size());
        return retMap;
    }

    public Object getAssemblyToolLocationDetail(InventoryManageDetailReq inventoryManageDetailReq) {
        Map<String,Object> retMap = new HashMap<>();
        Integer id = inventoryManageDetailReq.getAssemblyToolId();
        QueryWrapper<AssemblyToolInventoryEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("id" , id);
        AssemblyToolInventoryEntity assemblyToolInventoryEntity = assemblyToolInventoryMapper.selectOne(wrapper);
        //类型基础信息
        retMap.put("name",assemblyToolInventoryEntity.getToolNo());
        //总成材料清单信息
        List<Map<String, Object>> materialList = buildMaterialList(assemblyToolInventoryEntity);
        retMap.put("materialList",materialList);
        List<Map<String , Object>> inventorySnapshot = new ArrayList<>();
        List<String> dateList = toolInventoryLocationHistoryService.buildDateList();
        for (int i = dateList.size() - 2; i >= 0; i--) {
            Map<String , Object> locationInventoryStatusMap = new HashMap<>();
            Map<String, Integer> locationInventoryStatus = toolInventoryLocationHistoryRepository.locationInventoryStatus(dateList.get(i) , null , inventoryManageDetailReq.getAssemblyToolId() , inventoryManageDetailReq.getInventoryLocationId());
            locationInventoryStatusMap.put("date",dateList.get(i+1));
            locationInventoryStatusMap.put("inventoryStatus",locationInventoryStatus);
            inventorySnapshot.add(locationInventoryStatusMap);
        }
        Map<String,Object> nowDate = new HashMap<>();
        String currentDate = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        nowDate.put("date",currentDate);
        nowDate.put("inventoryStatus",toolInventoryLocationRespository.getlocationInventoryStatus(currentDate , null , inventoryManageDetailReq.getAssemblyToolId() , inventoryManageDetailReq.getInventoryLocationId()));
        inventorySnapshot.add(nowDate);
        retMap.put("dateList",inventorySnapshot);
        return retMap;
    }

    public Object queryNoManagementAssembleToolPart(ToolPartsManageReq toolPartsManageReq) {
        PageHelper.startPage(toolPartsManageReq.getPageNum() , toolPartsManageReq.getPageSize());
        PageInfo<ToolPartNoManagementVo> pageInfo = new PageInfo<>(toolPartRespository.queryNoManagementAssembleToolPart(toolPartsManageReq));
        if(CollectionUtil.isNotEmpty(pageInfo.getList())){
            for (ToolPartNoManagementVo toolPartNoManagementVo : pageInfo.getList()) {
                toolPartNoManagementVo.setImageUrl(minioProp.getBaseUrl()+minioProp.getBucketName()+"/"+assemblyToolInventoryRepository.selectImageByToolNo(toolPartNoManagementVo.getToolNo()));
            }
        }
        return new PageResult<>(pageInfo.getList(), pageInfo.getTotal());
    }

    public Object editToolPartLengthAndRadius(EditToolPartLengthAndRadiusReq editToolPartLengthAndRadiusReq) {
        toolPartRespository.editToolPartLengthAndRadius(editToolPartLengthAndRadiusReq);
        return Boolean.TRUE;
    }

    public Object qryAssemblyToolInfo(String rfid) {
        QueryWrapper<ToolPartEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("type_flag" , 0);
        wrapper.eq("unique_id" , rfid);
        ToolPartEntity toolPartEntity = toolPartMapper.selectOne(wrapper);
        if(ObjectUtil.isNotNull(toolPartEntity)&& toolPartEntity.getIsAssembled() == 1){
            QueryWrapper<ToolPartEntity> wrapper1 = new QueryWrapper<>();
            wrapper1.eq("type_flag" , 1);
            wrapper1.eq("unique_id" , toolPartEntity.getAssemblyToolUniqueId());
            ToolPartEntity assemblyToolEntity = toolPartMapper.selectOne(wrapper1);
            AssemblyToolInfoVo assemblyToolInfoVo = new AssemblyToolInfoVo();
            assemblyToolInfoVo.setToolNo(assemblyToolEntity.getToolNo());
            String params = assemblyToolEntity.getParams();
            assemblyToolInfoVo.setRfid(assemblyToolEntity.getUniqueId());
            assemblyToolInfoVo.setLength(assemblyToolEntity.getToolLength());
            assemblyToolInfoVo.setRadius(assemblyToolEntity.getRadius());
            buildToolPartParam("toolType" , assemblyToolInfoVo , params);
            buildToolPartParam("shankType" , assemblyToolInfoVo , params);
            LocationNameVo locationNameVo = inventoryLocationRespository.queryLocationByInventoryLocationId(assemblyToolEntity.getInventoryLocationId());
            if (locationNameVo != null) {
                StringBuilder stringBuilder = new StringBuilder();
                String locationName = stringBuilder.append(locationNameVo.getTypeName()).append("-").append(locationNameVo.getWarehouseName()).append("-库位").append(locationNameVo.getLocationNum()).append("-位置").append(locationNameVo.getRow()).append("-").append(locationNameVo.getColumn()).toString();
                assemblyToolInfoVo.setLocationName(locationName);
            }
            return assemblyToolInfoVo;
        }
        return Boolean.FALSE;
    }

    private void buildToolPartParam(String key , AssemblyToolInfoVo assemblyToolInfoVo , String params){
        if(StringUtils.isNotBlank(params)) {
            JSONObject jsonObject = JSONObject.parseObject(params);
            List<String> toolTypes = (List<String>) jsonObject.get(key);
            if(CollectionUtil.isNotEmpty(toolTypes)){
                QueryWrapper<ToolPartEntity> wrapper = new QueryWrapper<>();
                String rfid = toolTypes.get(0);
                wrapper.eq("unique_id" , rfid);
                ToolPartEntity toolPartEntity = toolPartMapper.selectOne(wrapper);
                if(ObjectUtil.isNotNull(toolPartEntity)) {
                    if (key.equals("toolType")) {
                        assemblyToolInfoVo.setToolType(toolPartEntity.getIdnr());
                        assemblyToolInfoVo.setToolRfid(toolPartEntity.getUniqueId());
                    } else if (key.equals("shankType")) {
                        assemblyToolInfoVo.setShankType(toolPartEntity.getIdnr());
                        assemblyToolInfoVo.setShankRfid(toolPartEntity.getUniqueId());
                    }
                }
            }
        }
    }

    public Object queryAssemblyTool(QueryAssemblyToolReq queryAssemblyToolReq){
        PageHelper.startPage(queryAssemblyToolReq.getPageNum() , queryAssemblyToolReq.getPageSize());
        QueryWrapper<AssemblyToolInventoryEntity> wrapper = new QueryWrapper<>();
        if(StringUtils.isNotBlank(queryAssemblyToolReq.getToolNo())){
            wrapper.like("tool_no" , queryAssemblyToolReq.getToolNo());
        }
        if(StringUtils.isNotBlank(queryAssemblyToolReq.getToolType())){
            wrapper.like("tool_type" , queryAssemblyToolReq.getToolType());
        }
        if(StringUtils.isNotBlank(queryAssemblyToolReq.getShankType())){
            wrapper.like("shank_type" , queryAssemblyToolReq.getShankType());

        }
        PageInfo<AssemblyToolInventoryEntity> pageInfo = new PageInfo<>(assemblyToolInventoryMapper.selectList(wrapper));
        return new PageResult<>(pageInfo.getList() , pageInfo.getTotal());
    }

    public Object addOrEditAssemblyTool(AddOrEditAssemblyToolReq addOrEditAssemblyToolReq) {
        if(ObjectUtil.isNotNull(addOrEditAssemblyToolReq.getId())){
            AssemblyToolInventoryEntity assemblyToolInventoryEntity = assemblyToolInventoryMapper.selectById(addOrEditAssemblyToolReq.getId());
            BeanUtil.copyProperties(addOrEditAssemblyToolReq , assemblyToolInventoryEntity);
            assemblyToolInventoryMapper.updateById(assemblyToolInventoryEntity);
        }
        return Boolean.TRUE;
    }

    public Object deleteAssemblyTool(Integer id) throws ToolLifeCycleException {
        if(ObjectUtil.isNull(id)){
            throw new ToolLifeCycleException(ToolLifeCycleEx.ASSEMBLY_TOOL_NO_EXIT.getCode() ,ToolLifeCycleEx.ASSEMBLY_TOOL_NO_EXIT.getMsg());
        }
        AssemblyToolInventoryEntity assemblyToolInventoryEntity = assemblyToolInventoryMapper.selectById(id);
        QueryWrapper<ToolInventoryLocationEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("assembly_tool_inventory_id" , id);
        List<ToolInventoryLocationEntity> assemblyToolInventoryEntities = toolInventoryLocationMapper.selectList(wrapper);
        if(CollectionUtil.isNotEmpty(assemblyToolInventoryEntities)){
            throw new ToolLifeCycleException(ToolLifeCycleEx.DELETE_EXCEPTION.getCode() ,ToolLifeCycleEx.DELETE_EXCEPTION.getMsg());
        }
        List<ToolPartEntity> resultList = inventoryLocationRespository.queryByToolNo(assemblyToolInventoryEntity.getToolNo());
        if(CollectionUtil.isNotEmpty(resultList)){
            throw new ToolLifeCycleException(ToolLifeCycleEx.DELETE_EXCEPTION.getCode() ,ToolLifeCycleEx.DELETE_EXCEPTION.getMsg());
        }
        assemblyToolInventoryMapper.deleteById(id);
        return Boolean.TRUE;
    }

    public Object countPhysicalInventory() {
        List<ToolInventoryLocationEntity> updateList = new ArrayList<>();
        List<ToolInventoryLocationEntity> toolInventoryLocationEntities = toolInventoryLocationMapper.selectList(new QueryWrapper<>());
        if(CollectionUtil.isNotEmpty(toolInventoryLocationEntities)){
            toolInventoryLocationEntities.forEach(toolInventoryLocationEntity -> {
                if(ObjectUtil.isNotNull(toolInventoryLocationEntity.getToolId())){
                    toolInventoryLocationEntity.setPartno(toolForkInfoRespository.selectPartnoById(toolInventoryLocationEntity.getToolId()));
                }
                if(ObjectUtil.isNotNull(toolInventoryLocationEntity.getAssemblyToolInventoryId())){
                    toolInventoryLocationEntity.setToolNo(assemblyToolInventoryRepository.selectToolNoById(
                            toolInventoryLocationEntity.getAssemblyToolInventoryId()));
                }
                toolInventoryLocationEntity.setPhysicalInventory(toolPartRespository.selectCountByCondition(toolInventoryLocationEntity
                        .getPartno() , toolInventoryLocationEntity.getToolNo() , toolInventoryLocationEntity.getInventoryLocationId()));
                updateList.add(toolInventoryLocationEntity);
            });
        }
        if(CollectionUtil.isNotEmpty(updateList)){
            toolInventoryLocationMapper.updateBatchById(updateList);
        }
        return Boolean.TRUE;
    }

    public Object toolReturnChart() {
        return null;
    }

    public Object checkToolNos() {
        List<String> strings = toolPartRespository.selectExistToolNos();
        QueryWrapper<AssemblyToolInventoryEntity> wrapper = new QueryWrapper<>();
        List<AssemblyToolInventoryEntity> assemblyToolInventoryEntities = assemblyToolInventoryMapper.selectList(new QueryWrapper<>());
        List<String> existToolNos = assemblyToolInventoryEntities
                .stream()
                .map(AssemblyToolInventoryEntity::getToolNo)
                .collect(Collectors.toList());
        List<String> toolNos = strings
                .stream()
                .filter(s ->!existToolNos.contains(s))
                .collect(Collectors.toList());
        return toolNos;
    }

    public Object testSyncData() throws ParseException {
        QueryWrapper<ICabinetRecordEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("operator" , "李汝俊");
        wrapper.eq("partno" , "4440001955");
        wrapper.eq("partType" , "HMX-4E-D4.0");
        wrapper.eq("locationNumber" , "7");
        wrapper.eq("pcs" , "5");
        wrapper.eq("number" , "5");
        List<ICabinetRecordEntity> iCabinetRecordEntities = iCabinetRecordMapper.selectList(wrapper);
        iCabinetRecordEntities.get(0).setTime(CommonUtil.parseDate("2024-04-24 22:33:33"));
//        ICabinetRecordEntity iCabinetRecordEntity = new ICabinetRecordEntity;
//        iCabinetRecordEntity.setNumber("5");
//        iCabinetRecordEntity.setTime("2024-04-23 22:33:33");
        List<Integer> warehouseId = inventoryLocationRespository.selectWarehouseIds();
        inventoryTask.syncRecordInventoryData( "1" , iCabinetRecordEntities , CommonUtil.getRandomElement(warehouseId));
        return iCabinetRecordEntities;
    }
}
